package com.gopay.bis.icbc.ebank.impl;

import java.util.Map;

import com.gopay.bis.icbc.ebank.operation.IcbcB2bQuery;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.icbc.ebank.pay.IcbcB2bMessage;
import com.gopay.common.domain.bank.icbc.ebank.query.res.CmsQueryB2BRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelperIcbc;
import com.gopay.remote.bank.businesses.BankService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 工行B2B请求接口
 * 
 * @IcbcB2bBankServiceImpl.java
 * @author WangHongFei
 */
public class IcbcB2bBankServiceImpl implements BankService {

	private ICertHelperIcbc icbcB2bCertHelper;
	private IcbcB2bQuery icbcB2bQuery;

	public IcbcB2bMessage sign(BaseMessage message) throws GopayException{
		IcbcB2bMessage messageb2b = (IcbcB2bMessage) message;
		Object objectRes = null;
		objectRes = icbcB2bCertHelper.sign(messageb2b);
		String signMessage = (String) objectRes;
		messageb2b.setSignMessage(signMessage);
		messageb2b.setCert(icbcB2bCertHelper.getPk(messageb2b.getPublicKeyPath()));
		return messageb2b;
	}

	public Object verify(BaseMessage message) throws GopayException{
		IcbcB2bMessage b2bMessage = (IcbcB2bMessage) message;
		Object objectRes = null;
		objectRes = icbcB2bCertHelper.verify(b2bMessage);
		return objectRes;
	}

	@SuppressWarnings("unchecked")
	public Object query(Object object) throws GopayException{
		Map<String, String> param = (Map<String, String>) object;
		String responseBody = icbcB2bQuery.sendReq(param);
		CmsQueryB2BRes res = new CmsQueryB2BRes();
		String plainMessage = icbcB2bQuery.decode(responseBody);
		XStream xs = new XStream(new DomDriver());
		xs.autodetectAnnotations(true);
		xs.toXML(res);
		res = (CmsQueryB2BRes) xs.fromXML(plainMessage);
		return res;
	}

	public void setIcbcB2bCertHelper(ICertHelperIcbc icbcB2bCertHelper) {
		this.icbcB2bCertHelper = icbcB2bCertHelper;
	}

	public void setIcbcB2bQuery(IcbcB2bQuery icbcB2bQuery) {
		this.icbcB2bQuery = icbcB2bQuery;
	}

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
